package com.core.base;


import com.alibaba.fastjson.JSONObject;

/**
 * @author gang
 */
public class Reply<T> {
    private boolean success;
    private String message;
    private Integer code;
    private T data;


    public boolean hasErrors() {
        return !success;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public static <T> Reply<T> failure(String message) {
        return failure(9999, message);
    }

    public static <T> Reply<T> failure(Integer code, String message) {
        Reply reply = new Reply();
        reply.setSuccess(false);
        reply.setMessage(message);
        reply.setCode(code);
        return reply;
    }

    public static Reply<Void> success() {
        return success(null);
    }

    public static <T> Reply<T> success(T data) {
        Reply reply = new Reply();
        reply.setCode(0);
        reply.setSuccess(true);
        reply.setData(data);
        return reply;
    }

    public static void main(String[] args) {

        Reply<String> re = Reply.success("成功了");
        String json = JSONObject.toJSONString(re);
        System.out.println(json);
    }

}
